#include <iostream>
#include<string>
#include<vector>

using namespace std;
int minOperations(vector<int>& nums, int x) {
    int m = nums.size();
    long long sum = 0;
    for (int i = 0; i < m; i++)
    {
        sum += nums[i];
    }

    if (x > sum)
        return -1;
    sum -= x;

    int left = 0;
    int right = 0;
    int len = 0;
    int target = nums[left];
    while (right < m)
    {
        if (target < sum)
        {
            right++;
            target += nums[right];




        }
        else if (target > sum)
        {
            target -= nums[left];
            left++;
        }
        else
        {
            if (right - left + 1 > len)
            {
                len = right - left + 1;


            }

            target -= nums[left];
            left++;




        }







    }
    return len == 0 ? -1 : m - len;





}
int main()
{
    vector<int> res = { 8828,9581,49,9818,9974,9869,9991,10000,10000,10000,9999,9993,9904,8819,1231,6309 };

    minOperations(res, 134365);




}
//
//int main() {
//    int ret;
//    cin >> ret;
//    string str = to_string(ret);
//    string result;
//    for (auto e : str)
//    {
//        if ((e - '0') % 2 == 0)
//            result.push_back('0');
//        else if ((e - '0') % 2 != 0)
//            result.push_back('1');
//
//    }
//    int res = stoi(result);
//    cout << res;
//
//
//
//
//
//
//
//}
class Solution {
public:
    int threeSumClosest(vector<int>& nums, int target) {
        int m = nums.size();
        sort(nums.begin(), nums.end());
        int result = INT_MAX;
        int returnsum = nums[0] + nums[1] + nums[2];


        for (int i = 0; i < m - 2; i++)
        {
            int left = i + 1, right = m - 1;
            while (left < right)
            {
                int sum = nums[i] + nums[left] + nums[right];
                if (abs(target - sum) < abs(target - returnsum))
                {
                    returnsum = sum;

                }
                if (sum < target)
                {

                    left++;

                }
                else if (sum > target)
                {

                    right--;
                }
                else
                {


                    return returnsum;


                }

            }

        }
        return returnsum;










    }
}; class Solution {
public:
    int totalFruit(vector<int>& f) {

        unordered_map<int, int>st;
        int left = 0;
        int right = 0;
        int len = 0;
        int m = f.size();
        while (right < m)
        {
            st[f[right]]++;
            while (st.size() > 2)
            {
                st[f[left]]--;
                if (st[f[left]] == 0)
                    st.erase(f[left]);
                left++;
            }
            len = max(len, right - left + 1);
            right++;
        }
        return len;





    }
}; class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int m = nums.size();
        long long sum = 0;
        for (int i = 0; i < m; i++)
        {
            sum += nums[i];
        }

        if (x > sum)
            return -1;
        if (x == sum)
            return m;
        sum -= x;

        int left = 0;
        int right = 0;
        int len = 0;
        int target = nums[left];
        while (right < m)
        {
            if (target < sum)
            {
                right++;
                target += nums[right];




            }
            else if (target > sum)
            {
                target -= nums[left];
                left++;
            }
            else
            {
                if (right - left + 1 > len)
                {
                    len = right - left + 1;


                }

                target -= nums[left];
                left++;




            }







        }
        return len == 0 ? -1 : m - len;





    }
}; class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int left = 0;
        int right = 0;
        int maxlenth = 0;
        int count = 0;
        int m = nums.size();
        while (right < m)
        {
            if (nums[right] == 0)count++;
            while (count > k)
                if (nums[left++] == 0)count--;
            maxlenth = max(maxlenth, right - left + 1);
            right++;






        }
        return maxlenth;
    }
};